package com.buydeem.log.serveltlog;

import lombok.Data;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author zengchao
 * @date 2021-05-07 11:30:30
 */
@RestController
public class TestController {

    @GetMapping("/get")
    public Object get(Integer id) {
        return id;
    }

    @PostMapping("/post/form")
    public Object postForm(String userName, String password, HttpServletRequest request) {
        return userName + password;
    }

    @PostMapping("/post/json")
    public Object postJson(@RequestBody User user) {
        return user;
    }

    @PostMapping("/file_upload")
    public Object uploadFile(@RequestParam("key") String key, @RequestParam("file") MultipartFile file) throws IOException {
        InputStream in = file.getInputStream();
        byte[] bytes = new byte[1];
        FileOutputStream out = new FileOutputStream("C:\\Users\\buydeem\\Desktop\\1.png");
        while ((in.read(bytes)) != -1) {
            out.write(bytes);
        }
        out.close();
        return key;
    }
}

@Data
class User {
    private String userName;
    private String password;
}
